#!
#Usage: ./blkparse.sh $fname $devname
blkparse ${2} -d sd-nvme.bin > /dev/null
btt -i sd-nvme.bin -l sd-nvme > btt.txt
safe_rm *.dat &
safe_rm sd-nvme.bin 1>/dev/null 2>&1 &

line_num=$( grep -n "Device Q2Q Seek Information" btt.txt | awk -F : '{print $1}' )
line_num=$(( $line_num - 1 ))
#clear
echo "" ; echo "***********************Block IO Traces Summary***********************"
head -n $line_num btt.txt
echo "***********************Block IO Summary End**************************"; echo ""

cat btt.txt | grep Q2C > temp.txt

if (( $find_exceed_io == 1 )); then
	workingpath=$(pwd)
	../../find_exceed_queue.sh -p $workingpath -d $2 -Q $Q2C_threshold -D $D2C_threshold
fi

Q2CAvg=`awk '{ print $3 }' ./temp.txt`

if echo $Q2CAvg | grep -qe "^[-\?[0-9]\+\.\?[0-9]*$"
then
    echo $Q2CAvg > /dev/null
else
    Q2CAvg=0
fi

cd ..
if (( $remain_data == 0 )); then
	safe_rm -rf "$1"
	exit 0
fi
tar -zcf ${1}_bak.tar.gz $1
safe_rm -rf "$1"

if [ $(echo "$Q2CAvg > $average_latency_threshold" | bc) -ne 0 ]; then
	cp ${1}_bak.tar.gz ${1}_exceed.tar.gz
fi
